package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.FaTieZiTask;
import com.xgkj.diyiketang.util.ToastUtil;

/**
 * Created by XCKJ_Paladowow on 2016/9/28.
 */
public class FaTieZiActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private EditText contentEdit;
    private String contentString;
    private EditText titleEdit;
    private String titleString;
    private TextView back;
    private TextView mPubBtn;
    private int createTaskId = -1;
    private String clubid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fatiezi);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if(intent!=null){
            clubid = intent.getStringExtra("CLUBID");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleEdit = (EditText) findViewById(R.id.title_content);
        contentEdit = (EditText) findViewById(R.id.jianjie_content);
        mPubBtn = (TextView) findViewById(R.id.publish);
        mPubBtn.setOnClickListener(this);
    }

    private boolean checkInput() {
        contentString = contentEdit.getText().toString();
        titleString = titleEdit.getText().toString();

        if (TextUtils.isEmpty(titleString)) {
            Toast.makeText(mContext, "标题不能为空",
                    Toast.LENGTH_SHORT).show();
            return false;
        }

        if (TextUtils.isEmpty(contentString)) {
            Toast.makeText(mContext, "内容不能为空",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.publish:

                if (checkInput()) {
                    doPublish();
                }
                break;
        }
    }

    private void doPublish() {
        FaTieZiTask ctask = new FaTieZiTask(clubid,titleString,contentString,"");
        ctask.addListener(fatieziTaskListener);
        createTaskId = getEatShowApp().getTaskManager().addTask(ctask);
    }

    private TaskEventListener fatieziTaskListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == createTaskId){
                ToastUtil.showMessage(mContext, "发表成功!");
                finish();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };
}
